﻿#region
/*******************************************************************************
** File Name:		    AdminUserService.cs
** Creator:		        chao.jiang 
** EMail:               chao.jiang 
** Create date:	        
** Latest Modifier:
** Latest Modify date:
** Description:		    
** Modify History:
**
** Code Review:
**
** Version number:       1.0.0
*********************************************************************************/
#endregion
using System;
using System.Collections.Generic;
using System.Text;
using Phoenix.Service.Contracts;
using Phoenix.Service.Model;
using Phoenix.IBatisRepository;
using Phoenix.Respository;
using System.Data;
using Phoenix.Entity;

namespace Phoenix.Service
{
    public class AdminUserService : IAdminUserService 
    {
		 IAdminUser adminUser = new AdminUserRepository();
		 
		 #region  Auto Generation
		 
        public void AddAdminUser(AdminUserModel model)
        {
			EAdminUser entity = ConvertModelToEntity(model);
			
            adminUser.AddAdminUser(entity);
        }

		public void UpdateByUserID (AdminUserModel model)
		{
			EAdminUser entity = ConvertModelToEntity(model);

            adminUser.UpdateByUserID(entity);

		}

		public void DeleteByUserID (System.Int32 userID)
		{
		
            adminUser.DeleteByUserID(userID);

		}
		 
        public AdminUserModel GetByUserID (System.Int32 userID)
        {
        
			EAdminUser entity = adminUser.GetByUserID(userID);
			
            AdminUserModel model = ConvertEntityToModel(entity);


            return model;
        }

        private AdminUserModel ConvertEntityToModel(EAdminUser entity)
        {
            if (entity == null)
            {
                return null;
            }
            AdminUserModel model = new AdminUserModel();
            model.UserID = entity.UserID;
            model.UserName = entity.UserName;
            model.Password = entity.Password;
            model.Sex = entity.Sex;
            model.EMail = entity.EMail;
            model.Telephone = entity.Telephone;
            model.Mobile = entity.Mobile;
            model.TrueName = entity.TrueName;
            model.Address = entity.Address;
            model.Memo = entity.Memo;
            model.Visitation = entity.Visitation;
            model.DataPermisType = entity.DataPermisType;
            model.DepartmentID = entity.DepartmentID;
            model.JobID = entity.JobID;
            model.JobLevel = entity.JobLevel;
            model.Level = entity.Level;
            model.Status = entity.Status;
            model.LockTime = entity.LockTime;
            model.LastLoginTime = entity.LastLoginTime;
            model.UpdateTime = entity.UpdateTime;
            model.CreateTime = entity.CreateTime;
            return model;
        }

        private EAdminUser ConvertModelToEntity(AdminUserModel model)
        {
            if (model == null)
            {
                return null;
            }
            EAdminUser entity = new EAdminUser();
            entity.UserID = model.UserID;
            entity.UserName = model.UserName;
            entity.Password = model.Password;
            entity.Sex = model.Sex;
            entity.EMail = model.EMail;
            entity.Telephone = model.Telephone;
            entity.Mobile = model.Mobile;
            entity.TrueName = model.TrueName;
            entity.Address = model.Address;
            entity.Memo = model.Memo;
            entity.Visitation = model.Visitation;
            entity.DataPermisType = model.DataPermisType;
            entity.DepartmentID = model.DepartmentID;
            entity.JobID = model.JobID;
            entity.JobLevel = model.JobLevel;
            entity.Level = model.Level;
            entity.Status = model.Status;
            entity.LockTime = model.LockTime;
            entity.LastLoginTime = model.LastLoginTime;
            entity.UpdateTime = model.UpdateTime;
            entity.CreateTime = model.CreateTime;
            return entity;
        }
		
		#endregion

        public IList<AdminUserModel> GetPagedList(int pageSize, int pageIndex, string trueName, string userName, DateTime? beginDate, DateTime? endDate, out int totalRecord)
        {
            IList<EAdminUser> entityList = adminUser.GetPagedList(pageSize, pageIndex, trueName,userName, beginDate,endDate,out totalRecord);

            if (entityList == null)
                return null;
            IList<AdminUserModel> modelList = new List<AdminUserModel>();
            
            foreach (var item in entityList)
            {
                modelList.Add(ConvertEntityToModel(item));
            }
            return modelList;
        }

        public void UpdateStatus(int userID, bool status)
        {
            adminUser.UpdateStatus(userID, status);

        }

        public AdminUserModel GetByUserName(string userName)
        {
            EAdminUser entity = adminUser.GetByUserName(userName);
            if (entity == null)
                return null;

            return ConvertEntityToModel(entity);

        }

        public bool GetExistsUserName(string userName)
        {
            return adminUser.GetExistsUserName(userName);
        }

        /// <summary>
        /// 批量修改部门级别
        /// </summary>
        /// <param name="newLevel">新部门级别</param>
        /// <param name="oldLevel">旧部门级别</param>
        public void BatchUpdateByDepartLevel(string newLevel, string oldLevel)
        {
            //string hsql = " Update EAccount  SET LEVEL =REPLACE(LEVEL,'," + oldLevel + "','," + newLevel + "') ";
            //HQuery hq = new HQuery(typeof(EAccount), hsql);

            //using (IRepository repository = SessionFactoryFactory.Default.CreateRepository())
            //{
            //    repository.Execute(hq);
            //}
            //removeCache();
        }

        
    }
}